<?php
App::uses('AppController', 'Controller');
/**
 * Benhhais Controller
 *
 * @property Benhhai $Benhhai
 * @property PaginatorComponent $Paginator
 */
class BenhhaisController extends AppController {

/**
 * Components
 *
 * @var array
 */
	public $components = array('Paginator');

/**
 * index method
 *
 * @return void
 */
	public function index() {
		$this->Benhhai->recursive = 0;
        debug($this->Paginator->paginate());
		$this->set('benhhais', $this->Paginator->paginate());
	}

/**
 * view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function view($id = null) {
		if (!$this->Benhhai->exists($id)) {
			throw new NotFoundException(__('Invalid benhhai'));
		}
		$options = array('conditions' => array('Benhhai.' . $this->Benhhai->primaryKey => $id));
		$this->set('benhhai', $this->Benhhai->find('first', $options));
	}

/**
 * add method
 *
 * @return void
 */
	public function admin_add() {
		if ($this->request->is('post')) {
			$this->Benhhai->create();
			if ($this->Benhhai->save($this->request->data)) {
				$this->Session->setFlash(__('Thêm thành công '),'backend/green',array('redirect'=>''));
				return $this->redirect(array('action' => 'admin_index'));
			} else {
				$this->Session->setFlash(__('Lỗi save dữ liệu'),'backend/red',array('redirect'=>''));
			}
		}
        $this->loadModel('Thuoc');
        $thuocs = $this->Thuoc->find('list',array('fields'=>array('id','ten_thuoc')));
        $this->loadModel('LoaiBenh');
        $loaibenhs = $this->LoaiBenh->find('list',array('fields'=>array('id','loai_benh')));
        $this->set(compact('thuocs','loaibenhs'));
	}

/**
 * edit method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function admin_edit($id = null) {
		if (!$this->Benhhai->exists($id)) {
			throw new NotFoundException(__('Invalid benhhai'));
		}
		if ($this->request->is('post') || $this->request->is('put')) {
			if ($this->Benhhai->save($this->request->data)) {
				$this->Session->setFlash(__('Cập nhật thành công '),'backend/green',array('redirect'=>''));
				
				return $this->redirect(array('action' => 'admin_index'));
			} else {
				$this->Session->setFlash(__('The benhhai could not be saved. Please, try again.'));
			}
		} else {
			$options = array('conditions' => array('Benhhai.' . $this->Benhhai->primaryKey => $id));
			$this->request->data = $this->Benhhai->find('first', $options);
            $this->loadModel('Thuoc');
            $thuocs = $this->Thuoc->find('list',array('fields'=>array('id','ten_thuoc')));
            $this->loadModel('LoaiBenh');
            $loaibenhs = $this->LoaiBenh->find('list',array('fields'=>array('id','loai_benh')));
            $this->set(compact('thuocs','loaibenhs'));
		}
	}

/**
 * delete method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function admin_delete($id = null) {
		$this->Benhhai->id = $id;
		if (!$this->Benhhai->exists()) {
			throw new NotFoundException(__('Invalid benhhai'));
		}
		$this->request->onlyAllow('get', 'admin_delete');
		if ($this->Benhhai->delete()) {
			$this->Session->setFlash(__('Xóa thành công'),'backend/green',array('redirect'=>''));
		} else {
			$this->Session->setFlash(__('Lỗi xóa dữ liệu'),'backend/red',array('redirect'=>''));
		}
		return $this->redirect(array('action' => 'admin_index'));
	}
    
    public function admin_index()
    {
       //$this->Benhhai->recursive = -1;
       $benhhais=$this->Paginator->paginate();
       $this->set('benhhais', $benhhais);
        
        
    }
    }
